package com.atguigu.tingshu;

import com.atguigu.tingshu.common.constant.RedisConstant;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.scheduling.annotation.EnableAsync;

@Slf4j
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)//取消数据源自动配置
@EnableDiscoveryClient
@EnableFeignClients//(basePackages = {"cn.", ""})
@EnableAsync
public class ServiceSearchApplication implements CommandLineRunner {

    @Autowired
    private RedissonClient redissonClient;


    public static void main(String[] args) {
        SpringApplication.run(ServiceSearchApplication.class, args);
    }

    /**
     * 启动项目自动触发一次，完成初始化布隆过滤器
     *
     * @param args incoming main method arguments
     * @throws Exception
     */
    @Override
    public void run(String... args) throws Exception {
        //1.创建布隆过滤器对象
        RBloomFilter<Long> bloomFilter = redissonClient.getBloomFilter(RedisConstant.ALBUM_BLOOM_FILTER);
        //2.判断布隆过滤器是否存在
        if (!bloomFilter.isExists()) {
            log.info("[搜索服务]初始化布隆过滤器..");
            //3.不存在，则基于期望数据规模跟误判率，完成初始化
            bloomFilter.tryInit(10000, 0.05);
        }
    }
}
